using Microsoft.Phone.Controls.LocalizedResources;
using Microsoft.Phone.Shell;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
namespace Microsoft.Phone.Controls.Primitives
{
	public abstract class DateTimePickerPageBase : PhoneApplicationPage, IDateTimePickerPage
	{
		private const string VisibilityGroupName = "VisibilityStates";
		private const string OpenVisibilityStateName = "Open";
		private const string ClosedVisibilityStateName = "Closed";
		private const string StateKey_Value = "DateTimePickerPageBase_State_Value";
		private LoopingSelector _primarySelectorPart;
		private LoopingSelector _secondarySelectorPart;
		private LoopingSelector _tertiarySelectorPart;
		private Storyboard _closedStoryboard;
		private DateTime? _value;
		public DateTime? Value
		{
			get
			{
				return this._value;
			}
			set
			{
				this._value = value;
				DateTimeWrapper selectedItem = new DateTimeWrapper(this._value.GetValueOrDefault(DateTime.get_Now()));
				this._primarySelectorPart.DataSource.SelectedItem = selectedItem;
				this._secondarySelectorPart.DataSource.SelectedItem = selectedItem;
				this._tertiarySelectorPart.DataSource.SelectedItem = selectedItem;
			}
		}
		protected void InitializeDateTimePickerPage(LoopingSelector primarySelector, LoopingSelector secondarySelector, LoopingSelector tertiarySelector)
		{
			if (primarySelector == null)
			{
				throw new ArgumentNullException("primarySelector");
			}
			if (secondarySelector == null)
			{
				throw new ArgumentNullException("secondarySelector");
			}
			if (tertiarySelector == null)
			{
				throw new ArgumentNullException("tertiarySelector");
			}
			this._primarySelectorPart = primarySelector;
			this._secondarySelectorPart = secondarySelector;
			this._tertiarySelectorPart = tertiarySelector;
			this._primarySelectorPart.DataSource.SelectionChanged += new EventHandler<SelectionChangedEventArgs>(this.OnDataSourceSelectionChanged);
			this._secondarySelectorPart.DataSource.SelectionChanged += new EventHandler<SelectionChangedEventArgs>(this.OnDataSourceSelectionChanged);
			this._tertiarySelectorPart.DataSource.SelectionChanged += new EventHandler<SelectionChangedEventArgs>(this.OnDataSourceSelectionChanged);
			this._primarySelectorPart.IsExpandedChanged += new DependencyPropertyChangedEventHandler(this.OnSelectorIsExpandedChanged);
			this._secondarySelectorPart.IsExpandedChanged += new DependencyPropertyChangedEventHandler(this.OnSelectorIsExpandedChanged);
			this._tertiarySelectorPart.IsExpandedChanged += new DependencyPropertyChangedEventHandler(this.OnSelectorIsExpandedChanged);
			this._primarySelectorPart.set_Visibility(1);
			this._secondarySelectorPart.set_Visibility(1);
			this._tertiarySelectorPart.set_Visibility(1);
			int num = 0;
			using (IEnumerator<LoopingSelector> enumerator = this.GetSelectorsOrderedByCulturePattern().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					LoopingSelector current = enumerator.get_Current();
					Grid.SetColumn(current, num);
					current.set_Visibility(0);
					num++;
				}
			}
			FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
			if (frameworkElement != null)
			{
				IEnumerator enumerator2 = VisualStateManager.GetVisualStateGroups(frameworkElement).GetEnumerator();
				try
				{
					while (enumerator2.MoveNext())
					{
						VisualStateGroup visualStateGroup = (VisualStateGroup)enumerator2.get_Current();
						if ("VisibilityStates" == visualStateGroup.get_Name())
						{
							IEnumerator enumerator3 = visualStateGroup.get_States().GetEnumerator();
							try
							{
								while (enumerator3.MoveNext())
								{
									VisualState visualState = (VisualState)enumerator3.get_Current();
									if ("Closed" == visualState.get_Name() && visualState.get_Storyboard() != null)
									{
										this._closedStoryboard = visualState.get_Storyboard();
										this._closedStoryboard.add_Completed(new EventHandler(this.OnClosedStoryboardCompleted));
									}
								}
							}
							finally
							{
								IDisposable disposable = enumerator3 as IDisposable;
								if (disposable != null)
								{
									disposable.Dispose();
								}
							}
						}
					}
				}
				finally
				{
					IDisposable disposable2 = enumerator2 as IDisposable;
					if (disposable2 != null)
					{
						disposable2.Dispose();
					}
				}
			}
			if (base.get_ApplicationBar() != null)
			{
				IEnumerator enumerator4 = base.get_ApplicationBar().get_Buttons().GetEnumerator();
				try
				{
					while (enumerator4.MoveNext())
					{
						object current2 = enumerator4.get_Current();
						IApplicationBarIconButton applicationBarIconButton = current2 as IApplicationBarIconButton;
						if (applicationBarIconButton != null)
						{
							if ("DONE" == applicationBarIconButton.get_Text())
							{
								applicationBarIconButton.set_Text(ControlResources.DateTimePickerDoneText);
								applicationBarIconButton.add_Click(new EventHandler(this.OnDoneButtonClick));
							}
							else
							{
								if ("CANCEL" == applicationBarIconButton.get_Text())
								{
									applicationBarIconButton.set_Text(ControlResources.DateTimePickerCancelText);
									applicationBarIconButton.add_Click(new EventHandler(this.OnCancelButtonClick));
								}
							}
						}
					}
				}
				finally
				{
					IDisposable disposable3 = enumerator4 as IDisposable;
					if (disposable3 != null)
					{
						disposable3.Dispose();
					}
				}
			}
			VisualStateManager.GoToState(this, "Open", true);
		}
		private void OnDataSourceSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			DataSource dataSource = (DataSource)sender;
			this._primarySelectorPart.DataSource.SelectedItem = dataSource.SelectedItem;
			this._secondarySelectorPart.DataSource.SelectedItem = dataSource.SelectedItem;
			this._tertiarySelectorPart.DataSource.SelectedItem = dataSource.SelectedItem;
		}
		private void OnSelectorIsExpandedChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			if ((bool)e.get_NewValue())
			{
				this._primarySelectorPart.IsExpanded = (sender == this._primarySelectorPart);
				this._secondarySelectorPart.IsExpanded = (sender == this._secondarySelectorPart);
				this._tertiarySelectorPart.IsExpanded = (sender == this._tertiarySelectorPart);
			}
		}
		private void OnDoneButtonClick(object sender, EventArgs e)
		{
			this._value = new DateTime?(((DateTimeWrapper)this._primarySelectorPart.DataSource.SelectedItem).DateTime);
			this.ClosePickerPage();
		}
		private void OnCancelButtonClick(object sender, EventArgs e)
		{
			this._value = default(DateTime?);
			this.ClosePickerPage();
		}
		protected override void OnBackKeyPress(CancelEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			e.set_Cancel(true);
			this.ClosePickerPage();
		}
		private void ClosePickerPage()
		{
			if (this._closedStoryboard != null)
			{
				VisualStateManager.GoToState(this, "Closed", true);
				return;
			}
			this.OnClosedStoryboardCompleted(null, null);
		}
		private void OnClosedStoryboardCompleted(object sender, EventArgs e)
		{
			base.get_NavigationService().GoBack();
		}
		protected abstract IEnumerable<LoopingSelector> GetSelectorsOrderedByCulturePattern();
		protected static IEnumerable<LoopingSelector> GetSelectorsOrderedByCulturePattern(string pattern, char[] patternCharacters, LoopingSelector[] selectors)
		{
			if (pattern == null)
			{
				throw new ArgumentNullException("pattern");
			}
			if (patternCharacters == null)
			{
				throw new ArgumentNullException("patternCharacters");
			}
			if (selectors == null)
			{
				throw new ArgumentNullException("selectors");
			}
			if (patternCharacters.Length != selectors.Length)
			{
				throw new ArgumentException("Arrays must contain the same number of elements.");
			}
			List<Tuple<int, LoopingSelector>> list = new List<Tuple<int, LoopingSelector>>(patternCharacters.Length);
			for (int i = 0; i < patternCharacters.Length; i++)
			{
				list.Add(new Tuple<int, LoopingSelector>(pattern.IndexOf(patternCharacters[i]), selectors[i]));
			}
			return Enumerable.Where<LoopingSelector>(Enumerable.Select<Tuple<int, LoopingSelector>, LoopingSelector>(Enumerable.OrderBy<Tuple<int, LoopingSelector>, int>(Enumerable.Where<Tuple<int, LoopingSelector>>(list, (Tuple<int, LoopingSelector> p) => -1 != p.Item1), (Tuple<int, LoopingSelector> p) => p.Item1), (Tuple<int, LoopingSelector> p) => p.Item2), (LoopingSelector s) => null != s);
		}
		protected override void OnNavigatedFrom(NavigationEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnNavigatedFrom(e);
			if ("app://external/" == e.get_Uri().ToString())
			{
				base.get_State().set_Item("DateTimePickerPageBase_State_Value", this.Value);
			}
		}
		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnNavigatedTo(e);
			if (base.get_State().ContainsKey("DateTimePickerPageBase_State_Value"))
			{
				this.Value = (base.get_State().get_Item("DateTimePickerPageBase_State_Value") as DateTime?);
				if (base.get_NavigationService().get_CanGoBack())
				{
					base.get_NavigationService().GoBack();
				}
			}
		}
	}
}
